package com.example.zwh.computershop.commodity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Commodity_goods extends AppCompatActivity {

    //搜索结果
    String search_result;
    @BindView(R.id.Commodity_btn_gotoback)
    LinearLayout CommodityBtnGotoback;
    @BindView(R.id.Commodity_honme_search)
    LinearLayout CommodityHonmeSearch;
    @BindView(R.id.lay_Commodity_search_name)
    LinearLayout layCommoditySearchName;
    //全部的数据
    private ArrayList<Product> mproducts = new ArrayList<>();
    //搜索结果的数据
    private ArrayList<Product> search_mproducts = new ArrayList<>();
    int state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_goods);
        ButterKnife.bind(this);
        search_result = getIntent().getStringExtra("search_result");//搜索的内容
        initlayout();
    }

    @OnClick({R.id.Commodity_btn_gotoback, R.id.lay_Commodity_search_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Commodity_btn_gotoback:
                onBackPressed();//返回上一个活动
                break;
            case R.id.lay_Commodity_search_name:
                startActivity(new Intent(this, Home_Activity.class));//点击返回首页
                break;
        }
    }

    //导入视图
    private void initlayout() {

        getPro();//服务器导入数据
        Commodity_display();

        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_search_result_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        if (state == 3) {
            search(search_result);//执行搜索
            Search_Adapter search_adapter = new Search_Adapter(search_mproducts,Commodity_goods.this);
            recyclerView.setAdapter(search_adapter);
        }
    }

    //布局处理 title  键盘
    private void Commodity_display() {
        //顶部菜单栏屏蔽
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        //隐藏键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    //导入本地的数据 不要了
    private void initData() {
        Product huawei = new Product();
        Product hp = new Product();
        for (int i = 0; i < 5; i++) {
            huawei.setProductName("HUAWEI MateBook 13 " + i);
            huawei.setImgUrl(Integer.toString(R.drawable.huawei_goods_shopp));
            huawei.setProductInfor("HUAWEI MateBook 13系列于2018年上市，搭载英特尔8代处理器，MX150显卡，win10系统");
            huawei.setPrice(4999.0);
            mproducts.add(huawei);
            hp.setProductName("惠普星 14  " + i);
            hp.setImgUrl(Integer.toString(R.drawable.hp_news));
            hp.setProductInfor("惠普 星 14系列于2018年上市，搭载英特尔8代处理器，窄边框屏幕，时尚轻薄本 ");
            hp.setPrice(3999.0);
            mproducts.add(hp);
        }
    }

    //服务器拿数据了
    public void getPro() {
        Thread t = new Thread(new takeThread());
        t.start();
        try {
            t.join();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    //数据线程
    class takeThread extends Thread {
        @Override
        public void run() {
            state = handleData.getPro(mproducts);//拿数据的ArrayList类型数组mproducts
            //if 就成功了
            if (state == 3) {

            }
        }
    }

    //执行搜索 并将结果存在search_mproducts中
    public void search(String search_result) {
        //创建迭代器
        Iterator<Product> productIterator = mproducts.iterator();
        while (productIterator.hasNext()) {
            Product mproduct = productIterator.next();
//            Log.e("Shenyutian", "search: "+mproduct.getProductName());
            if (mproduct.getProductName().contains(search_result)) {
                search_mproducts.add(mproduct);
            }
        }
    }

}
